﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract Izvadak typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class IzvadakDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<IzvadakDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Izvadak _currentIzvadak = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxIzvadakDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxIzvadakErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxIzvadakBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the DatumIzradeIzvatka property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDatumIzradeIzvatkaDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the BrojDepozitne property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxBrojDepozitneDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Datoteka property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDatotekaDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDKnjizenje property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDKnjizenjeDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Knjizenje> _IDKnjizenjeList;
		
		/// <summary> 
		/// The list of selectable Knjizenje
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Knjizenje> IDKnjizenjeList
		{
			get {return this._IDKnjizenjeList;}
			set 
			{
				this._IDKnjizenjeList = value;
				this.uxIDKnjizenjeDataGridViewColumn.DataSource = null;
				this.uxIDKnjizenjeDataGridViewColumn.DataSource = this._IDKnjizenjeList;
			}
		}
		
		private bool _allowNewItemInIDKnjizenjeList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Knjizenje
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDKnjizenjeList
		{
			get { return _allowNewItemInIDKnjizenjeList;}
			set
			{
				this._allowNewItemInIDKnjizenjeList = value;
				this.uxIDKnjizenjeDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Izvadak> _IzvadakList;
				
		/// <summary> 
		/// The list of Izvadak to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Izvadak> IzvadakList
		{
			get {return this._IzvadakList;}
			set
			{
				this._IzvadakList = value;
				this.uxIzvadakBindingSource.DataSource = null;
				this.uxIzvadakBindingSource.DataSource = value;
				this.uxIzvadakDataGridView.DataSource = null;
				this.uxIzvadakDataGridView.DataSource = this.uxIzvadakBindingSource;				
				//this.uxIzvadakBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxIzvadakBindingSource_ListChanged);
				this.uxIzvadakBindingSource.CurrentItemChanged += new System.EventHandler(OnIzvadakBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnIzvadakBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentIzvadak = uxIzvadakBindingSource.Current as Entities.Izvadak;
			
			if (_currentIzvadak != null)
			{
				_currentIzvadak.Validate();
			}
			//_Izvadak.Validate();
			OnCurrentEntityChanged();
		}

		//void uxIzvadakBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Izvadak"/> instance.
		/// </summary>
		public Entities.Izvadak SelectedIzvadak
		{
			get {return this._currentIzvadak;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxIzvadakDataGridView.VirtualMode;}
			set
			{
				this.uxIzvadakDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxIzvadakDataGridView.AllowUserToAddRows;}
			set {this.uxIzvadakDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxIzvadakDataGridView.AllowUserToDeleteRows;}
			set {this.uxIzvadakDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxIzvadakDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxIzvadakDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="IzvadakDataGridViewBase"/> class.
		/// </summary>
		public IzvadakDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxIzvadakDataGridView = new System.Windows.Forms.DataGridView();
			this.uxIzvadakBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxIzvadakErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDatumIzradeIzvatkaDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxBrojDepozitneDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDatotekaDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDKnjizenjeDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			//this.uxIDKnjizenjeBindingSource = new KnjizenjeBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKnjizenjeBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIzvadakDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIzvadakBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIzvadakErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxIzvadakErrorProvider
			// 
			this.uxIzvadakErrorProvider.ContainerControl = this;
			this.uxIzvadakErrorProvider.DataSource = this.uxIzvadakBindingSource;						
			// 
			// uxIzvadakDataGridView
			// 
			this.uxIzvadakDataGridView.AutoGenerateColumns = false;
			this.uxIzvadakDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxIzvadakDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxDatumIzradeIzvatkaDataGridViewColumn,
		this.uxBrojDepozitneDataGridViewColumn,
		this.uxDatotekaDataGridViewColumn,
		this.uxIDKnjizenjeDataGridViewColumn			});
			this.uxIzvadakDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxIzvadakDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxIzvadakDataGridView.Name = "uxIzvadakDataGridView";
			this.uxIzvadakDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxIzvadakDataGridView.TabIndex = 0;	
			this.uxIzvadakDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxIzvadakDataGridView.EnableHeadersVisualStyles = false;
			this.uxIzvadakDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnIzvadakDataGridViewDataError);
			this.uxIzvadakDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnIzvadakDataGridViewCellValueNeeded);
			this.uxIzvadakDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnIzvadakDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxDatumIzradeIzvatkaDataGridViewColumn
			//
			this.uxDatumIzradeIzvatkaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDatumIzradeIzvatkaDataGridViewColumn.DataPropertyName = "DatumIzradeIzvatka";
			this.uxDatumIzradeIzvatkaDataGridViewColumn.HeaderText = "DatumIzradeIzvatka";
			this.uxDatumIzradeIzvatkaDataGridViewColumn.Name = "uxDatumIzradeIzvatkaDataGridViewColumn";
			this.uxDatumIzradeIzvatkaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDatumIzradeIzvatkaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDatumIzradeIzvatkaDataGridViewColumn.ReadOnly = false;		
			//
			// uxBrojDepozitneDataGridViewColumn
			//
			this.uxBrojDepozitneDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxBrojDepozitneDataGridViewColumn.DataPropertyName = "BrojDepozitne";
			this.uxBrojDepozitneDataGridViewColumn.HeaderText = "BrojDepozitne";
			this.uxBrojDepozitneDataGridViewColumn.Name = "uxBrojDepozitneDataGridViewColumn";
			this.uxBrojDepozitneDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxBrojDepozitneDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxBrojDepozitneDataGridViewColumn.ReadOnly = false;		
			//
			// uxDatotekaDataGridViewColumn
			//
			this.uxDatotekaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDatotekaDataGridViewColumn.DataPropertyName = "Datoteka";
			this.uxDatotekaDataGridViewColumn.HeaderText = "Datoteka";
			this.uxDatotekaDataGridViewColumn.Name = "uxDatotekaDataGridViewColumn";
			this.uxDatotekaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDatotekaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDatotekaDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDKnjizenjeDataGridViewColumn
			//
			this.uxIDKnjizenjeDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDKnjizenjeDataGridViewColumn.DataPropertyName = "IDKnjizenje";
			this.uxIDKnjizenjeDataGridViewColumn.HeaderText = "IDKnjizenje";
			this.uxIDKnjizenjeDataGridViewColumn.Name = "uxIDKnjizenjeDataGridViewColumn";
			this.uxIDKnjizenjeDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDKnjizenjeDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDKnjizenjeDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDKnjizenjeDataGridViewColumn
			//				
			this.uxIDKnjizenjeDataGridViewColumn.DisplayMember = "Datum";	
			this.uxIDKnjizenjeDataGridViewColumn.ValueMember = "ID";	
			this.uxIDKnjizenjeDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDKnjizenjeDataGridViewColumn.DataSource = uxIDKnjizenjeBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxIzvadakDataGridView);
			this.Name = "IzvadakDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKnjizenjeBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIzvadakErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIzvadakDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxIzvadakBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				IzvadakDataGridViewEventArgs args = new IzvadakDataGridViewEventArgs();
				args.Izvadak = _currentIzvadak;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class IzvadakDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Izvadak	_Izvadak;
	
			/// <summary>
			/// the  Entities.Izvadak instance.
			/// </summary>
			public Entities.Izvadak Izvadak
			{
				get { return _Izvadak; }
				set { _Izvadak = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxIzvadakDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnIzvadakDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxIzvadakDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnIzvadakDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxIzvadakDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = IzvadakList[e.RowIndex].ID;
						break;
					case "uxDatumIzradeIzvatkaDataGridViewColumn":
						e.Value = IzvadakList[e.RowIndex].DatumIzradeIzvatka;
						break;
					case "uxBrojDepozitneDataGridViewColumn":
						e.Value = IzvadakList[e.RowIndex].BrojDepozitne;
						break;
					case "uxDatotekaDataGridViewColumn":
						e.Value = IzvadakList[e.RowIndex].Datoteka;
						break;
					case "uxIDKnjizenjeDataGridViewColumn":
						e.Value = IzvadakList[e.RowIndex].IDKnjizenje;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxIzvadakDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnIzvadakDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxIzvadakDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						IzvadakList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxDatumIzradeIzvatkaDataGridViewColumn":
						IzvadakList[e.RowIndex].DatumIzradeIzvatka = (System.DateTime)e.Value;
						break;
					case "uxBrojDepozitneDataGridViewColumn":
						IzvadakList[e.RowIndex].BrojDepozitne = (System.Int32)e.Value;
						break;
					case "uxDatotekaDataGridViewColumn":
						IzvadakList[e.RowIndex].Datoteka = (System.Byte[])e.Value;
						break;
					case "uxIDKnjizenjeDataGridViewColumn":
						IzvadakList[e.RowIndex].IDKnjizenje = (System.Int32?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
